package com.atguigu.edu.realtime.util;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.common.EduConfig;
import org.apache.flink.api.java.tuple.Tuple2;
import redis.clients.jedis.Jedis;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Lec
 * @date 2022/8/28 16:29
 */

public class DimUtil {
    // select * from t where id = 'v.id' and user_id = '10'
    public static JSONObject getDimInfo(Connection connection, String tableName, String id) {
        JSONObject result = null;
//        String redisKey = "DIM:" + tableName + ":" + id;
//        // TODO 优先读取redis
//        Jedis jedis = null;
//        String value = null;
//        try {
//            jedis = JedisUtil.getJedis();
//            value = jedis.get(redisKey);
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println("jedis异常");
//        }
//
//        if (value != null) {
//            // TODO 如果redis中有存储的数据  直接返回
//            result = JSONObject.parseObject(value);
//        } else {
            // TODO 如果redis中没有存储的数据  查询phoenix
            List<JSONObject> dimInfo = getDimInfo(connection, tableName, new Tuple2<>("ID", id));/*这里id要是大写的，因为Phoenix中是大写的，看下面的方法中*/
            if (dimInfo.size() > 0) {
                result = dimInfo.get(0);
                // TODO 写入到redis中
//                jedis.setex(redisKey, 3600 * 24, result.toJSONString());
            }

//        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

//        jedis.close();
        return result;
    }

    public static List<JSONObject> getDimInfo(Connection connection, String tableName, Tuple2<String, String>... tuple2s) {
        StringBuilder sql = new StringBuilder();
        sql.append("select * from ")
                .append(EduConfig.HBASE_SCHEMA)
                .append(".")
                .append(tableName)
                .append(" where ");
        for (int i = 0; i < tuple2s.length; i++) {
            sql.append(tuple2s[i].f0)
                    .append(" = '")
                    .append(tuple2s[i].f1)
                    .append("'");
            if (i < tuple2s.length - 1) {
                sql.append(" and ");
            }
        }

//        System.out.println(sql.toString());

        return PhoenixUtil.sqlQuery(connection, sql.toString(), JSONObject.class);
    }

    public static void deleteRedisCache(String tableName, String id) {
        String redisKey = "DIM:" + tableName + ":" + id;

//        String type = value.getString("type");
//        if ("update".equals(type)) {
        try {
            Jedis jedis = JedisUtil.getJedis();
            jedis.del(redisKey);
            jedis.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("jedis异常");
        }
//        }

    }
}
